package com.czmh.etc.onlinepubsys.utils;

import java.io.UnsupportedEncodingException;

import org.apache.commons.codec.DecoderException;

import com.czmh.etc.onlinepubsys.dto.ObuInfo;

/**
 * @描述： @author： Seven.
 * 
 * @创建日期：Dec 30, 2019 5:48:57 PM. @版本：1.0.0
 */
public class ObuInfoUtil {
    /**
     * 
     * @param info
     * @return
     * @throws DecoderException 
     * @throws UnsupportedEncodingException 
     */
    public static ObuInfo decodeObuInfo(ObuInfo info) throws UnsupportedEncodingException, DecoderException {
        info.setCardId(StringHexUtil.getChinese(StringHexUtil.hexStr2Str(info.getCardId(), "GB2312")));
        info.setCardType(String.valueOf(Long.parseLong(info.getCardType(), 16)).equals("22") ? "储值卡" : "记账卡");
        info.setCardVersion(String.valueOf(Long.parseLong(info.getCardVersion(), 16)));
        info.setSn(StringHexUtil.hexStr2Str(info.getSn(), "GB2312").substring(4));
        String carplate = StringHexUtil.hexStr2Str(info.getCarPlate(), "GB2312").substring(0, StringHexUtil.hexStr2Str(info.getCarPlate(), "GB2312").length() - 4);
        info.setCarPlate(carplate);
        info.setUserType("个人用户");
        String color = String.valueOf(Long.parseLong(info.getCarPlateColor(), 16));
        if ("0".equals(color)) {
            info.setCarPlateColor("蓝");
        } else if ("1".equals(color)) {
            info.setCarPlateColor("黄");
        } else if ("2".equals(color)) {
            info.setCarPlateColor("黑");
        } else if ("3".equals(color)) {
            info.setCarPlateColor("白");
        } else if ("4".equals(color)) {
            info.setCarPlateColor("渐变绿色");
        } else if ("5".equals(color)) {
            info.setCarPlateColor("黄绿双拼");
        } else if ("6".equals(color)) {
            info.setCarPlateColor("蓝白渐变");
        }
        info.setBattery(info.getBattery().substring(info.getBattery().length() - 2).equals("50") ? "电量正常" : "电量低");
        String deviceVersion = "";
        String versionStr = info.getDeviceVersion().substring(info.getDeviceVersion().length() - 4);
        String version1 = versionStr.substring(0, 2);
        String version2 = versionStr.substring(versionStr.length() - 2);
        deviceVersion = String.valueOf(Long.parseLong(version1, 16)) + "." + String.valueOf(Long.parseLong(version2, 16));
        info.setDeviceVersion(deviceVersion);
        info.setCarType("客车");
        
        String ef01 = info.getEf01();
        int m = ef01.length() / 2;
        if (m * 2 < ef01.length()) {
            m++;
        }
        String[] strs = new String[m];
        int j = 0;
        for (int i = 0; i < ef01.length(); i++) {
            if (i % 2 == 0) {// 每隔两个
                strs[j] = "" + ef01.charAt(i);
            } else {
                strs[j] = strs[j] + ef01.charAt(i);
                j++;
            }
        }
        String serialNumber = "";
        for (int i = 10; i < 18; i++) {
            serialNumber = serialNumber + strs[i];
        }
        String splitState = strs[28];
        info.setSerialNumber(serialNumber);
        info.setSplitState("00".equals(splitState) ? "标签已失效" : "标签正常");
        info.setStartTime(info.getStartTime().substring(0, 4) + "年" + info.getStartTime().substring(4, 6) + "月" + info.getStartTime().substring(6, 8) + "日");
        info.setEndTime(info.getEndTime().substring(0, 4) + "年" + info.getEndTime().substring(4, 6) + "月" + info.getEndTime().substring(6, 8) + "日");
        return info;
    }
}
